#include "ShortListMenu.h"
#include "StringUtilities.h"
#include "Utilities.h"
#include "GamePrintUtils.h"
#include "PrintUtilities.h"
#include "MenuManager.h"
#include "ServiceProvider.h"
#include "TeamManager.h"
#include "PlayerMenu.h"
#include "UserManager.h"
#include "User.h"
#include "People.h"
#include "PeopleManager.h"

ShortListMenu::ShortListMenu(MenuManager* const i_menuManager)
	: Menu(i_menuManager)
{
}

void ShortListMenu::OnEnter()
{
	const UsersList& ul = GetSP()->GetUserMgr().GetUserList();
	m_user = 0;
	for(size_t i=0; i<ul.size(); ++i)
	{
		if(ul[i]->m_team == m_team)
		{
			m_user = ul[i];
			break;
		}
	}
	assert(m_user);

	IntVec& slIndexes = m_user->m_shortlist;
	m_shortlist.clear();
	m_shortlist.reserve(slIndexes.size());
	for(size_t i=0; i<slIndexes.size(); ++i)
	{
		People* people = GetSP()->GetPeopleMgr().GetPeopleFromID(slIndexes[i]);
		if(people)
			m_shortlist.push_back(people);
		else
			slIndexes[i] = Utilities::INVALID_ID;
	}
	Utilities::RemoveAll(slIndexes, Utilities::INVALID_ID);
}

void ShortListMenu::Draw() const
{
	STD::cout << Print::Title("SHORTLIST MENU'");

	using STD::cout;
	using STD::endl;
	using Utilities::TabUpTo;
	
	const int maxPlayerNameSize = STL::min(People::GetParams().MAX_CHAR_NAME, 25u);
	const int maxTeamNameSize = STL::min(Team::GetParams().MAX_CHAR_TEAM_NAME, 25u);

	cout << endl << endl;
	STL::string options;
	if(!m_shortlist.empty())
	{
		cout << TabUpTo("N", 3);
		cout << TabUpTo("NAME", maxPlayerNameSize);
		cout << TabUpTo("YS", 3);
		cout << TabUpTo("ATK", 4);
		cout << TabUpTo("DEF", 4);
		cout << TabUpTo("GLK", 4);
		cout << TabUpTo("RES", 4);
		cout << TabUpTo("AGG", 4);
		cout << TabUpTo("SAL", 5);
		cout << TabUpTo("BUY", 6);
		cout << TabUpTo("NM", 3);
		cout << TabUpTo("EVAL", 5);
		cout << TabUpTo("MM", 3);
		cout << TabUpTo("GL", 4);
		cout << TabUpTo("RAT", 5);
		cout << TabUpTo("", 2);
		cout << TabUpTo("TEAM", maxTeamNameSize);
		cout << endl << endl;

		for(size_t i=0; i<m_shortlist.size(); ++i)
		{
			const People& thePlayer = *m_shortlist[i];
			cout << TabUpTo(i+1, 3);
			cout << TabUpTo(thePlayer.GetName().substr(0, maxPlayerNameSize-1), maxPlayerNameSize);
			cout << TabUpTo(thePlayer.GetHistory().GetPlayerAge(), 3);
			cout << TabUpTo(thePlayer.GetSkills().m_playerSkills[Attacker], 4);
			cout << TabUpTo(thePlayer.GetSkills().m_playerSkills[Defender], 4);
			cout << TabUpTo(thePlayer.GetSkills().m_playerSkills[Goalkeeper], 4);
			cout << TabUpTo(thePlayer.GetSkills().m_physicsSkills[Resistance], 4);
			cout << TabUpTo(thePlayer.GetSkills().m_mentalSkills[Aggressivity], 4);
			cout << TabUpTo(thePlayer.GetSalary(), 5);
			cout << TabUpTo(thePlayer.BuyValue(), 6);
			cout << TabUpTo(thePlayer.GetHistory().GetMatch(), 3);
			cout << TabUpTo(thePlayer.GetHistory().GetEvalStr(), 5);
			cout << TabUpTo(thePlayer.GetHistory().GetMoM(), 3);
			cout << TabUpTo(thePlayer.GetHistory().GetScoredGoal(), 4);
			cout << TabUpTo(Utilities::RatioToString(thePlayer.GetHistory().GetScoredGoalRatio()), 5);
			cout << TabUpTo("", 2);
			cout << TabUpTo(GetTeamName(thePlayer, GetSP()->GetTeamMgr().GetTeamName(thePlayer.GetTeamID())).substr(0,maxTeamNameSize-1), maxTeamNameSize);
			cout << endl;
		}
		options = ("1-"+Utilities::IntToStlString(m_shortlist.size())+") People Info.\n");
	}
	else
	{
		cout << "No players in the shortlist!\n";
	}
	cout << endl << endl;
	options += "\n0) Return to previous menu'.";
	STD::cout  << Print::Options(options) << "Selection: ";;
}

bool ShortListMenu::Input(const STL::string& i_input)
{
	bool goodInput = true;

	int res = Utilities::StringToInt(i_input);

	if (res > 0 && res <= static_cast<int>(m_shortlist.size()))
	{
		PlayerMenu* nextMenu = static_cast<PlayerMenu*>(GetMM()->GetPlayerMenu());
		nextMenu->SetPlayer(m_shortlist[res-1]);
		GetMM()->PushMenu(nextMenu);
	}
	else if (res == 0)
	{
		GetMM()->PopMenu();
	}

	return goodInput;
}
